﻿<?php
/**
 *  本程序几个关键的校验：
 * 1：不能看别人的东西
 * TODO2：数据上限控制
 * 3：各种情况的消息提示
 * @author 老杨
 *
 */
class GuestAction extends MY_Controller {
	
	/**
	 * 构造函数
	 */
	function GuestAction() {
		parent::MY_Controller ();
		$this->load->model('user/guestmodel', "guestmodel", true);
	}
	
	/**
	 * 邀请人管理首页
	 * @param $offset
	 * @param $msg_id
	 */
	function index($offset='0', $msg_id='-1') {
		$data['guest_list'] = $this->guestmodel->get_guests_by_userid($this->session->userdata('user_id'), 0);
		$data['total_count'] =  $this->guestmodel->get_count_guests_by_userid($this->session->userdata('user_id'));
		if ($msg_id != -1) {
			$data['msg'] = $this->_msg($msg_id); 
		}
		$this->load->view("user/guest_index", $data);
	}
	
	/**
	 * ajax 列表 
	 */
	function ajax_list() {
		$data['guest_list'] = $this->guestmodel->get_guests_by_userid($this->session->userdata('user_id'), $this->input->post('offset'));
		$this->load->view("user/guest_list_ajax", $data);
	}
	
	/**
	 * 查询初始化
	 * @param unknown_type $b_i_category 大分类参数
	 * @param unknown_type $i_category 小分类参数
	 * @param unknown_type $i_flag 邀请、未邀请
	 */
	function search($b_i_category = "", $i_category = "", $i_flag = "") {
		$condition['b_i_category'] = $b_i_category == 0 ? '' : $b_i_category;
		$condition['i_category'] = $i_category == 0 ? '' : $i_category;
		$condition['i_flag'] = $i_flag;
		$data['condition']= $condition;
		$this->load->view("user/guest_search", $data);
	}
	/**
	 * ajax查询
	 */
	function ajax_search() {
		$form_data = $this->_form_data();
		$form_data['user_id'] = $this->session->userdata('user_id');
		$data['guest_list'] = $this->guestmodel->get_guests_by_condition($form_data); 
		$this->load->view("user/guest_list_ajax", $data);	
	}
	/**
	 * ajax总数据量
	 */
	function ajax_count() {
		$form_data = $this->_form_data();
		$form_data['user_id'] = $this->session->userdata('user_id');
		echo $this->guestmodel->get_count_by_condtion($form_data);
	}
	
	/**
	 * 编辑初始化
	 * @param unknown_type $id 修改时所用的id
	 */
	function edit_init($id = false, $msg = false) {
		$this->load->helper('form');
		if ($msg) {
			$data["msg"] = $this->_msg($msg);
		}
		//修改
		if ($id) {
			$data['guest'] = $this->guestmodel->get_one($id);
			if ($data['guest']['user_id'] != $this->session->userdata('user_id')) {
				redirect("user/guestAction/index/0/2");
				return;
			}
			$data['button'] = $this->_create_button("2");
			$this->load->view('user/guest_edit', $data);
		} else {
			//新增
			$data['button'] = $this->_create_button("1");
			$this->load->view('user/guest_edit', $data);
		}
	}
	
	
	
	/**
	 * 编辑
	 */
	function edit() {
		//+----------------------+
		//| 加载表单类、表单校验类 |
		//+----------------------+
		$this->load->helper(array('form'));
  		$this->load->library('form_validation');
		$this->form_validation->set_rules('i_name', '邀请人姓名', 'trim|required|max_length[5]|xss_clean');
		$this->form_validation->set_rules('phone', '电话', 'trim|max_length[20]|xss_clean');
		$this->form_validation->set_rules('i_category', '邀请人类型', 'trim|required');
		$this->form_validation->set_rules('remark', '备注', 'trim|max_length[50]|xss_clean');
		$this->form_validation->set_rules('money', '预测礼金', 'trim|numeric');
		if ($this->form_validation->run() == FALSE) {
			//+----------------------+
			//| 显示编辑页面                          |
			//+----------------------+
			$data['guest'] =  $this->_form_data();
			if ($this->input->post('id') == "") {
				$data['button'] = $this->_create_button("1");
			} else {
				$data['button'] = $this->_create_button("2");
			}
			$this->load->view('user/guest_edit', $data);
		} else {
			//+----------------------+
			//| 新增处理                                    |
			//+----------------------+
			if ($this->input->post('id') == "") {
				$guest = $this->_form_data(array('id', 'action'));
				$guest['user_id'] = $this->session->userdata('user_id');

				$this->guestmodel->save_guest($guest);
				if ($this->input->post('action') == 'l') {
					redirect("user/guestAction/index/0/1");
				}
				if ($this->input->post('action') == 's') {
					redirect("user/guestAction/edit_init/0/1");
				}
			} else {
				//+----------------------+
				//| 修改处理                                    |
				//+----------------------+
				$guest = $this->_form_data(array('action'));
				$guest['user_id'] = $this->session->userdata('user_id');

				$this->guestmodel->update_guest($guest);
				redirect("user/guestAction/index/0/3");
			}
		}
	}
	
	/**
	 * 报表
	 */
	function chart() {
		$this->load->view('user/guest_chart');
	}
	
	function create_chart() {
		
		$this->load->library('guestmoneychart');
		echo $this->guestmoneychart->create_chart();
	}
	
	/**
	 * 创建前台表单提交所使用的按钮
	 * @param unknown_type $type 1是新增  2是修改
	 * @return string|string
	 */
	function _create_button($type) {
		if($type == 1) {
			return $str = '<button class="my_button" title="save_to_list" type="submit" id="guset_update">保存跳转到列表</button> &nbsp;&nbsp;&nbsp;&nbsp;<button class="my_button" title="save" type="submit" id="guset_update">保存继续添加</button>';
		} else {
			return $str = '<button class="my_button" title="save_to_list" type="submit" id="guset_update">修改邀请嘉宾</button>';
		}
	}
	
	function del($id = '0') {
		echo "删除";
	}
	
	/**
	 * 产生消息的函数
	 * @param $msg_id 消息id
	 */
	function _msg($msg_id) {
		$msg = "";
		switch ($msg_id) {
			case '1': $msg = '<span style="color: red;padding-bottom:20px">保存成功！</span>';
				break;
			case '2': $msg = '<span style="color: red;padding-bottom:20px">您不能查看别人的信息、谢谢合作！</span>';
				break;
			case '3': $msg = '<span style="color: red;padding-bottom:20px">修改成功！</span>';
				break;	
			default:
				$msg = "";
		}
		return $msg;
	}

}
/* End of file GuestAction.php */
/* Location: ./system/application/controllers/GuestAction.php */